define(['bootstrapTable','layer','formValidate'],function (bootstrapTable,layer,formValidate) {
    return{
        init:function () {
            //先初始化一次
            window.current = 0;
            window.total = 0;
            window.symbol = 1;

            var today = getNowFormatDate();
            $("#calledDate").html(today);

            showData();

            /*定时刷新页面*/
            setInterval(function () {
                showData();
            },10000)
        }
    };
    function showData(){
        var today = getNowFormatDate2();
        $.ajax({
            url:"/statisticsController/findByModal",
            type:"post",
            data: {
                sceneCode: "52011501",
                calledDate: today
            },
            success:function(data){
                var result = typeof data === "string" ? JSON.parse(data) : data;
                if (result.code === 10001) {
                    //$("#calledDate").html(result.data.calledDate);
                    //$("#current").html(result.data.people);
                    //$("#total").html(result.data.total);
                    listenUpdate(result.data);
                } else{
                    layer.msg(result.msg);
                }
            }
        });
    }

    /**
     * 监听数据变化
     * 实时人数：可以上下浮动
     * 累计人数：只能增加
     */
    function listenUpdate(obj) {
        if (obj != null && obj.hasOwnProperty("people") && obj.hasOwnProperty("total")) {
            if (window.people == obj.people && window.total == obj.total) {
                // 还未更新
                // 可以操作继续增加
                domUpdate(obj);
            } else {
                // 处理更新
                domUpdate(obj);
                window.people = parseInt(obj.people);
                window.total = parseInt(obj.total);
            }
        } else {
            // 发生异常了
        }
    }

    /**
     * 更新DOM
     */
    function domUpdate(obj) {
        var random = rand(10);
        var symbol = window.symbol;

        if (obj != null && obj.hasOwnProperty("people")) {
            var current = $("#current").html();
            if (window.people != obj.people) {
                // 更新：取实际的数据
                $("#current").html(obj.people);
            } else {
                // 随机 加、减
                var differ = parseInt(current) - obj.people;
                var middle = parseInt(current) + symbol * random;
                if (-20 < differ && differ < 20 && middle > 0) {
                    $("#current").html(middle);
                    window.symbol = -1 * symbol;
                }else {
                    //console.log(">>回到真实值");
                    $("#current").html(obj.people);
                }

            }

        }

        if (obj != null && obj.hasOwnProperty("total")) {
            var total = $("#total").html();
            var differ = parseInt(total) - obj.total;

            if (window.people != obj.people) {
                $("#total").html(obj.total);
            } else {
                // 与真实数据相差50以上的才操作加
                if (symbol > 0 && differ < 50) {
                    //$("#total").html(obj.total);
                    $("#total").html(parseInt(total) + random);
                }
            }

        }
    }

    /**
     * 产生一个 n 以内的随机数
     */
    function rand(n) {
        return Math.floor(Math.random() * n + 1);
    }
    function getNowFormatDate() {
        var date = new Date();
        //var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + ' 年 ' + month + ' 月 ' + strDate + ' 日';
        return currentdate;
    }
    function getNowFormatDate2() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
});